package com.onemileshop.service;

import java.util.ArrayList;

import com.onemileshop.app.R;
import com.onemileshop.app.UILogin;
import com.onemileshop.app.UIMain;
import com.onemileshop.entiy.PubConst;
import com.onemileshop.entiy.Session;
import com.onemileshop.entiy.UrlParams;
import com.onemileshop.entiy.model.EMsgHead;
import com.onemileshop.entiy.model.EPostData;
import com.onemileshop.entiy.model.EPostResult;
import com.onemileshop.net.NetCheckNewOrder;
import com.onemileshop.utils.UtiLog;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.text.format.Time;
import android.util.Log;

public class RevCheckNewOrder extends BroadcastReceiver
{
   @Override
   public void onReceive(final Context context, Intent intent)
   {
	  if (intent.getAction().equals("oms.alarm.checkneworder"))
	  {
		 try
		 {
			PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
			final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeUp");
			wl.acquire();
			NetCheckNewOrder task = new NetCheckNewOrder(new EMsgHead(UrlParams.Shop_CheckNewOrder.APICode), new NetCheckNewOrder.ITaskCallBack()
			{
			   @Override
			   public void DoWhenComplete(EPostResult result)
			   {
				  if (result != null)
				  {
					 if (result.getResult().toString().equals("true"))
		            {
						NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
						Notification notification = new Notification(R.drawable.app_icon, "又有新的订单了！", System.currentTimeMillis());
						
						Intent intent=new Intent();
						intent.setClass(context, UILogin.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						notification.setLatestEventInfo(context, "新订单消息", "点击查看新订单消息", PendingIntent.getActivity(context, 0, intent,0));

						notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击后消失
						notification.defaults |= Notification.DEFAULT_SOUND; // 设置声音
						manager.notify(1, notification); // 触发事件时，会不会重复显示多个消息
					 }
				  }
			   }
			});

			ArrayList<EPostData> list = new ArrayList<EPostData>();
			SharedPreferences sp = context.getSharedPreferences(PubConst.Setting.Setting, Activity.MODE_PRIVATE);
			list.add(new EPostData(UrlParams.Shop_CheckNewOrder.ShopId, intent.getStringExtra(UrlParams.Shop_CheckNewOrder.ShopId)));
			list.add(new EPostData(UrlParams.Shop_CheckNewOrder.OrderNumber, String.valueOf(sp.getInt(UrlParams.Shop_CheckNewOrder.OrderNumber,0))));
			task.execute(list);
			wl.release();
		 }
		 catch (Exception e)
		 {
			if (Session.getInstance().IsDEBUG())
			{
			   Log.e("ERROR", "OMSService_run()", e);
			}
		 }
	  }
   }
}
